'use strict';

const Service = require('egg').Service;

class DishService extends Service {
  async createDish(name,description,picUrl,type,price) {
    var head = ''
    switch (type) {
        case 1:
            head = 'R';
            break;
        case 2:
            head = 'L';
            break;
        case 3:
            head = 'T';
            break;
        case 4:
            head = 'Z';
            break;
        default:
            break;
    }
    const code = head +String(Date.parse(new Date())/1000);
    const star = 0;
    // const createTime = new Date();
    const selling = 1;

    try{
        const result =await this.app.mysql.insert('dish',{
            code:code,
            name:name,
            description:description,
            pic:picUrl,
            star:star,
            selling:selling,
            type:type,
            price:price
        })
        const rid = this.ctx.request.headers['rid'];
        const relation = await this.app.mysql.insert('restaurant_dish',{rid:rid,did:result.insertId});
        if(result.affectedRows === 1 && relation.affectedRows === 1){
            return true;
        }else{
            return false;
        }
    }catch (err){
        return false;
    }
  }
  async dishList(){
    const rid = this.ctx.request.headers['rid'];
    try {
        const result = await this.app.mysql.query('SELECT dish.* FROM restaurant_dish INNER JOIN dish ON restaurant_dish.did = dish.id WHERE restaurant_dish.rid = ?',[rid]);
        return result;
    }catch(err){
        return 'error';
    }
  }

  async changeSellStatus(did,status){
      try{
          const result = await this.app.mysql.update('dish',{id:did,selling:status});
          if(result.affectedRows === 1){
              return 'success';
          }else{
              return 'error'
          }
      }catch(err){
          return err;
      }
  }

  async deleteDish(did){
      try{
          const result = await this.app.mysql.delete('dish',{id:did});
          const relation = await this.app.mysql.delete('restaurant_dish',{did:did});
          if (result.affectedRows === 1 && relation.affectedRows === 1){
              return 'success';
          }else{
              return 'error';
          }
      }catch(err){
          return err;
      }
  }

  async getOnSaleList(page,size){
    const rid = this.ctx.request.headers['rid'];
      try{
        const list = await this.app.mysql.query('SELECT dish.* from restaurant_dish \
                                                INNER JOIN dish ON restaurant_dish.did = dish.id \
                                                WHERE restaurant_dish.rid = ? AND dish.selling = 1 \
                                                ORDER BY dish.created DESC LIMIT ?,?',[rid,(page-1)*size,size]);

        const sum = await this.app.mysql.query('SELECT COUNT(1) s FROM restaurant_dish \
        INNER JOIN dish ON restaurant_dish.did = dish.id WHERE dish.selling = 1');

        return {sum:sum[0].s,list:list};
      }catch(err){
          return 'error';
      }
  }

  async changePrice(did,price){
      try{
          const result = await this.app.mysql.update('dish',{
              id:did,
              price:price
          });
          if(result.affectedRows === 1){
              return 'success';
          }else{
              return 'error';
          }
      }catch(err){
          return err.message;
      }
  }

}

module.exports = DishService;
